import path from "path";
import { BrowserWindow, MessageChannelMain } from "electron";

process.env.DIST_ELECTRON = path.join(__dirname, "..");
process.env.DIST = path.join(process.env.DIST_ELECTRON, "../dist");
process.env.PUBLIC = process.env.VITE_DEV_SERVER_URL
  ? path.join(process.env.DIST_ELECTRON, "../public")
  : process.env.DIST;
// Here, you can also use other preload
const preload = path.join(__dirname, "../preload/index.js");
const url = process.env.VITE_DEV_SERVER_URL;
const indexHtml = path.join(process.env.DIST, "index.html");
function createWindow(hash = "/", opt = {}) {
  const win = new BrowserWindow(
    Object.assign(
      {
        icon: path.join(process.env.PUBLIC, "favicon.ico"),
        width: 800,
        height: 600,
        minWidth: 800,
        minHeight: 600,
        autoHideMenuBar: true,
        webPreferences: {
          // contextIsolation: false,
          // nodeIntegration: true,
          // nodeIntegrationInWorker: true,
          //webSecurity: false,
          preload
        }
      },
      opt
    )
  );
  if (process.env.VITE_DEV_SERVER_URL) {
    win.loadURL(`${url}#${hash}`);
    //win.webContents.openDevTools({ mode: "right", activate: true });
  } else {
    win.loadFile(indexHtml, {
      hash
    });
  }
  // Test active push message to Renderer-process.
  win.webContents.on("did-finish-load", () => {
    win.webContents.send("main-process-message", new Date().toLocaleString());
  });
  win.webContents.session.setProxy({
    mode: "system"
  });
  return win;
}

export { createWindow };
